home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group01b.txt
/
000052_icon-group-sender_Wed Mar 7 13:38:30 2001.msg
< prev
next >
Wrap
Internet Message Format
|
2002-01-03
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id f27KcRU24299
for icon-group-addresses; Wed, 7 Mar 2001 13:38:27 -0700 (MST)
Message-Id: <200103072038.f27KcRU24299@baskerville.CS.Arizona.EDU>
From: "Geoff Summerhayes" <sNuOmSrPnAoMt@hNoOtSmPaAiMl.com>
X-Newsgroups: comp.lang.apl,comp.lang.forth,comp.lang.icon,comp.lang.lisp,comp.lang.mumps,comp.lang.scheme,comp.lang.smalltalk
Subject: Re: New Scientist Puzzle
Date: Wed, 7 Mar 2001 15:32:00 -0500
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
X-Complaints-To: newsabuse@supernews.com
To: icon-group@cs.arizona.edu
Errors-To: icon-group-errors@cs.arizona.edu
Status: RO
Content-Length: 1907
"WildHeart'2k1" <stf+inetnews@apl.it> wrote in message
news:985niv$4qa4$1@stargate1.inet.it...
>
>
> The prolog solution is interesting because it almost literally (in English)
> implement the statement of the problem.
>
Unfortunately, the Prolog solution has a flaw in it that isn't exposed because
the problem only has one solution. write/1 doesn't allow backtracking, the
program as it stood only produced the first solution it came across.
My revised solution follows, I've altered the digit-picking logic to do the
comparisons automatically instead of having to hand-code them, takes longer
but is scalable. create_number/3 is also more general than before.
I've also altered some of the variable names to make the logic a little clearer.
leading_digit(X,L,[X|L]) :-
member(X,[1,2,3,4,5,6,7,8,9]),
\+ member(X,L).
digit(X,L,[X|L]) :-
member(X,[0,1,2,3,4,5,6,7,8,9]),
\+ member(X,L).
pure_square(A) :-
B is round(sqrt(A)), A is B * B.
create_number([],N,N).
create_number([H|T],N,NR):-
N1 is N*10+H,
create_number(T,N1,NR).
passes_criteria(V,I,E,R,N,U,VIER,NEUN) :-
create_number([V,I,E,R],0,VIER),
create_number([N,E,U,N],0,NEUN),
pure_square(VIER),pure_square(NEUN).
possible_solutions(VIER,NEUN) :-
leading_digit(V,[],L), leading_digit(N,L,L1),
digit(I,L1,L2), digit(E,L2,L3),
digit(R,L3,L4), digit(U,L4,_),
passes_criteria(V,I,E,R,N,U,VIER,NEUN).
count_matches(_,[],0).
count_matches([X,Y],[[X,_]|T],V):-!,count_matches([X,Y],T,V1),V is V1 + 1.
count_matches([X,Y],[[_,Y]|T],V):-!,count_matches([X,Y],T,V1),V is V1 + 1.
count_matches(A,[_|T],V):-count_matches(A,T,V).
solve(V,N):-
bagof([X,Y],possible_solutions(X,Y),Possibles),
member([V,N],Possibles),
count_matches([V,N],Possibles,Count),
1 is Count.
% d:/prolog/lisp.prolog compiled 0.00 sec, 5,780 bytes
?- solve(VIER,NEUN).
VIER = 6241
NEUN = 9409 ;
No
?-
---Geoff---